<?php
/**
 * 功能描述：	通用文章管理
 * @datetime	2022-07-12
 * @version   	v2.3
 * @author   	ZHL
 * @copyrigh  	2022 ZHL 版权所有
 */

class Admin_SideController extends Controller {

    private $CLASS_NAME;
    private $CLASS_FUNS;



    /**
     * Notes: 文章子集列表
     * User: ZhuHaili
     * Date: 2022/8/27
     */
    public function index(){
        // 接收参数
        $mouldCode = Fun::input('m');
        $menu_id = Fun::input('c', 0, 'd');
        $essayId = Fun::input('a', 0, 'd');
        $pageNo = Fun::input('p', 1, 'd');
        $fid = Fun::input('f', 0, 'd');
        $findLevel = Fun::input('l', 0, 'd');
        $search = isset($_POST['search']) ? json_decode($_POST['search'], true) : array();
        $menuSer = new Admin_MenuServe();
        $classInfo = $menuSer->getMenuInfo(array('id'=>$menu_id));
        if(empty($classInfo)){ htmlAlert('当前菜单已经不存在'); }
        // 文章列表
        $essaySer = new Admin_EssayServe();
        $result = $essaySer->getEssaySideList($mouldCode, $essayId, $pageNo, $fid, $search);
        if(!is_array($result)){
            htmlAlert($result);
        }
        // 表关联字段
        $fieldSer = new Admin_FieldServe();
        $fields1 = $fieldSer->getOpenClassFieldsInfos($menu_id,'2','3');
        // 子父层级控制
        if(!$findLevel){
            $findLevel = intval($classInfo['sideflag']) ? 1 : 0;
        }else{
            $findLevel = intval($classInfo['sideflag']) > $findLevel ? $findLevel+1 : 0;
        }
        // 映射
        $this->set('statusList', BaseData::$statusArr2);
        $this->set('fieldFileType', array_keys(BaseData::$fieldTypeArrFile));
        $this->set('fieldPicType', array_keys(BaseData::$fieldTypeArrPic));
        $this->set('fieldBtnType', array_keys(BaseData::$fieldTypeArrBtn));
        $this->set('mouldCode', $mouldCode);
        $this->set('classInfo', $classInfo);
        $this->set('menu_id', $menu_id);
        $this->set('essayId', $essayId);
        $this->set('fid', $fid);
        $this->set('findLevel', $findLevel);
        $this->set('userLevel', getUserLevel());
        $this->set('fields', $fields1);
        $this->set('searchInfo', $result['where']);
        $this->set('list', $result['list']);
        $this->set('pages', $result['pages']);
        $this->set('title', '列表');
        $this->display();
    }

    /**
     * Notes:文章子集导出Execl表格
     * User: ZhuHaili
     */
    function execl(){
        $mouldCode = Fun::input('m');
        $menu_id = Fun::input('c', 0, 'd');
        $essay_id = Fun::input('a', 0, 'd');
        $pageNo = Fun::input('p', 0, 'd');
        $fid = Fun::input('f', 0, 'd');
        $idStr = Fun::input('i');
        $search = Fun::input('search');
        $orderby = Fun::input('o'); //排序例如：a.id_desc,a.viewseq_desc
        if($orderby){$orderby = strtr($orderby, '_', " ");} // 替换字符串
        // 获取数据
        $essaySer = new Admin_EssayServe();
        $result = $essaySer->getEssaySideLists($mouldCode, $menu_id, $essay_id, $fid, $idStr, $search, $orderby, $pageNo);
        // 导出execl
        $commM = new Admin_CommonModel();
        $essayInfo = $commM->getEssayInfo($mouldCode, array('id'=>$essay_id));
        $fileNameStr = isset($essayInfo['title']) ? $essayInfo['title'] : $essay_id;
        $fileTitle = $fileNameStr.'_子集_'.date('Ymd-His'); // execl文件名
        $e = array('id','title'); // 屏蔽科学计数法字段
        $csv = new ManageCSV();
        $csv->export($result['trList'], $result['thList'], $fileTitle, $e);
    }

    /**
     * Notes:文章添加
     * User: ZhuHaili
     */
    public function add(){
        $this->setEssaySide('add', 'add');
    }

    /**
     * Notes:文章修改
     * User: ZhuHaili
     */
    public function update(){
        $this->setEssaySide('update', 'update');
    }

    /**
     * Notes:文章子集添加 / 修改
     * User: ZhuHaili
     * Date: 2022/8/10
     * @param $param
     * @param $tmpl
     */
    public function setEssaySide($setFlag, $tmpl){
        $param = Fun::input();
        $subFlag = Fun::input('subFlag','');  // 数据来源标识
        //## 来自提交数据时
        if($subFlag == 'Y'){
            $essaySer = new Admin_EssayServe();
            $foreachNo = isset($param['foreachno']) && intval($param['foreachno']) ? intval($param['foreachno']) : 1; // 循环添加次数
            $title = isset($param['title']) ? $param['title'] : '';
            for($i = 0; $i < $foreachNo; $i ++){
                if($title){
                    $param['title'] = $i ? ($i+1).$title : $title;
                }
                $result = $essaySer->setEssaySide($param, $setFlag);
            }
            Fun::json($result, true);
        }
        //## 来自页面预览时
        $setData['mouldCode'] = Fun::input('m','');    // 模型编码
        $setData['menu_id'] = Fun::input('c','');      // 菜单id
        $setData['menu_id'] = $setData['menu_id'] ? $setData['menu_id'] : Fun::input('menu_id','0');      // 菜单id
        $setData['fid'] = Fun::input('f','');          // 父级ID
        $setData['id'] = Fun::input('i','');           // 子集id
        $setData['essay_id'] = Fun::input('a','');     // 文章id
        $commModel = new Admin_CommonModel();
        //栏目
        $setData['menuInfo'] = $commModel->getEssayInfo('sysmenu', array('id'=>$setData['menu_id']));
        // 详情
        $setData['info'] = array();
        // 修改操作时
        if($setFlag == 'update'){
            // 当前详情
            $setData['info'] = $commModel->getEssayInfo($setData['mouldCode'].'_side', array('id'=>$setData['id']));
            if(empty($setData['info'])){ htmlAlert('当前内容已经不存在'); }
        }
        // 文章开放字段
        $fieldSer = new Admin_FieldServe();
        $fields = $fieldSer->getOpenClassFieldsInfos($setData['menu_id'], '2', '3');
        $fieldItems = FieldItemAdd::setItems($fields, $setData['info']);
        $setData['fieldItemHtml'] = isset($fieldItems['html']) ? $fieldItems['html'] : '';
        $setData['fieldItemStyle'] = isset($fieldItems['style']) ? $fieldItems['style'] : '';
        // 映射
        foreach($setData as $k => $v){
            $this->set($k, $v);
        }
        $this->set('title', $setData['mouldCode'].'_side_'.$setData['menu_id'].'_'.$setData['essay_id']);
        $this->display($tmpl);
    }

    /**
     * Notes:日志删除
     * User: ZHL
     * Date: 2022/7/18
     */
    public function del(){
        $m = Fun::input('m');
        $id = Fun::input('i', 0, 'd');
        $commS = new Admin_CommonServe();
        $result = $commS->del($m.'_side', $id);
        Fun::json($result, true);
    }

    /**
     * Notes:批量删除
     * User: ZHL
     * Date: 2022/7/18
     */
    public function dels(){
        $m = Fun::input('m');
        $idStr = Fun::input('i');
        $commS = new Admin_CommonServe();
        $result = $commS->dels($m.'_side', $idStr);
        Fun::json($result, true);
    }

    /**
     * Notes:批量修改状态
     * User: ZhuHaili
     */
    public function status(){
        $m = Fun::input('m');
        $newStatus = Fun::input('s');
        $idStr = Fun::input('i');
        $commS = new Admin_CommonServe();
        $result = $commS->batchUpdateStatus($m.'_side', $idStr, $newStatus);
        Fun::json($result, true);
    }

    /**
     * Notes:批量排序
     * User: ZHL
     */
    public function seq(){
        $m = Fun::input('m');
        $m = $m.'_side';
        $commC = new Admin_CommonController();
        $commC->seq($m);
    }
}